package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.VO.SpecVO;
import com.dongdongshop.domain.DataResult;
import com.dongdongshop.em.DataResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoogsService;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.service.TbSpecificationService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("SelleritemController")
public class SelleritemController {

    @Reference
    private TbItemCatService tbItemCatService;
    @Reference
    private TbGoogsService tbGoogsService;
    @Reference
    private TbSpecificationService tbSpecificationService;

    @RequestMapping("tomanagement")
    public String tomanagement(){
        return "/admin/goods_edit";
    }

    @RequestMapping("selectsjld")
    @ResponseBody
    public DataResult selectsjld(Long id){
        List<TbItemCat> tbItemCats = tbItemCatService.selectById(id);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(tbItemCats);
    }
    @RequestMapping("selectByid")
    @ResponseBody
    public DataResult selectByid(Long id){
        TbItemCat tbItemCat = tbItemCatService.select(id);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(tbItemCat);
    }

    @RequestMapping("gettemplate")
    @ResponseBody
    public DataResult gettemplate(Long id){
        TbTypeTemplate tbTypeTemplate = tbItemCatService.gettemplateById(id);
        String brandIds = tbTypeTemplate.getSpecIds();
        List<SpecVO> specVOS = JSONObject.parseArray(brandIds, SpecVO.class);
        for(SpecVO specVO:specVOS){
            List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationService.selectByids(specVO.getId());
            specVO.setOptionList(tbSpecificationOptions);
        }
        Map<String,Object> result = new HashMap<>();
        result.put("tbTypeTemplate",tbTypeTemplate);
        result.put("specVOS",specVOS);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(result);
    }

    @RequestMapping("insertGoods")
    @ResponseBody
    public DataResult insertadd(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc){
        TbSeller t =  (TbSeller)SecurityUtils.getSubject().getPrincipal();
        tbGoods.setSellerId(t.getSellerId());
        Boolean b = tbGoogsService.insertinto(tbGoods,tbGoodsDesc);
        return DataResult.buildDataResult(DataResultEnum.SUCCESS);
    }

    @RequestMapping("selectSpec")
    @ResponseBody
    public DataResult selectSpec(Long id){
        try {
            TbTypeTemplate tbTypeTemplate = tbItemCatService.selectByIds(id);
            String specIds = tbTypeTemplate.getSpecIds();
            List<Map> maps = JSONObject.parseArray(specIds, Map.class);
            List<SpecVO> specVoList = new ArrayList<>();
            for (Map map : maps) {
                Long specId = Long.valueOf((Integer)map.get("id"));
                String specName = (String) map.get("text");
                List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationService.selectBySpecId(specId);
                SpecVO specVo = new SpecVO();
                specVo.setId(specId);
                specVo.setText(specName);
                specVo.setOptionList(tbSpecificationOptions);
                specVoList.add(specVo);
            }
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(specVoList);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }

    }

}
